*==========================================================================================================================================================
*
*	Local Projections
*
*==========================================================================================================================================================


set more off

qui {


*-------------------------------------------------------------------------------------------------------------
*	SETTINGS
*-------------------------------------------------------------------------------------------------------------

* Variables
local vars "essentials_index nonessentials_index processed_index p_soap_index p_oliveoil_index p_candles_index p_charcoal_index p_firewood_index p_uwo_ns_index p_iron p_nai_index p_coarse_index p_fine_index p_threads_index" 

* irf horizon
local H 5

* rhs controls lags
local L 2 

*-------------------------------------------------------------------------------------------------------------



*_________ GET DATA ____________
use "data/working files/prepared.dta", clear 


*____ DECLARE TIME SERIES _____
gen id =1
tsset id year, yearly


*_________________ LOCAL PROJECTIONS _________________

local count = 0
foreach v of local vars {
local count = `count'+1
n di "Variable `v'"

***_________ CONTROLS

local indep	" gdp_r_adj_gr wage_sil_gr itsN_gr" 

forvalues i=0/`L'{
	local indep_`i' = subinstr("`indep'"," "," l`i'_",.)
	}
	
local rhs ""
forvalues i = 1/`L'{
	local rhs "`rhs'`indep_`i''"
	}
	
	local not l0_`v'_gr l0_itsN_gr
	local rhs: list rhs- not
	

	***_________ IMPULSE RESPONSE and CONFIDENCE INTERVAL variables
	gen irf_`v' = . if _n<=`H'+1
	
	gen se_`v' = . if _n<=`H'+1

	gen sig_`v' = . if _n<=`H'+1
	gen ci_up_`v' = . if _n<=`H'+1
	gen ci_lo_`v' = . if _n<=`H'+1
	gen ci_up90_`v' = . if _n<=`H'+1
	gen ci_lo90_`v' = . if _n<=`H'+1
				
	
	***_________ ESTIMATE COEFFICIENTS
	
	forvalues y = 0/`H' {
		n display "projection `y'"
		
		sum `v' 
		if (`r(N)' < 100) {
			local F = 0
			local LS = 1
			newey `v'_gr`y' l(1/`LS').`v'_gr l(-`F'/`LS').percloss_netN, lag(5) force
		}

		sum `v'
		if (`r(N)' >= 100) {
			local F = 5
			local LS = 2
			newey `v'_gr`y' l(1/`LS').`v'_gr l(-`F'/`LS').perclossN_vellon l(-`F'/`LS').perclossN l(-`F'/`LS').percloss_salvN `rhs' l(-`F'/`LS').percloss_pirN l(0/`LS').temperature l(0/`LS').warbeu l(0/`LS').warbciv, lag(5) force
			
		}
	
		* store IMPULSE RESPONSES and STANDARD ERRORS	
		sum `v'
		if (`r(N)' >= 100) {
			lincom perclossN
		}
		else{
		lincom percloss_netN 
		}
		replace irf_`v' = r(estimate) if _n==`y'+1 /* coefficients */
		replace se_`v' = r(se) if _n==`y'+1 /* standard errors */
		

		} //horizon			
	} // vars

	
local count = 0
foreach v of local vars {
local count = `count'+1
n di "Variable `v'"

	***_________ CONFIDENCE INTERVALS
	replace ci_up_`v' = irf_`v' + 1*se_`v' if _n <= `H'+1
	replace ci_lo_`v' = irf_`v' - 1*se_`v' if _n <= `H'+1
	replace ci_up90_`v' = irf_`v' + 1.64*se_`v' if _n <= `H'+1
	replace ci_lo90_`v' = irf_`v' - 1.64*se_`v' if _n <= `H'+1
	
	replace sig_`v' =  irf_`v' if ci_up90_`v' < 0 | ci_lo90_`v' > 0

} // vars

* plot prerequisites
gen time = _n-1 if(_n<=`H'+1) 
drop if time > `H'
keep time irf_* ci_up_* ci_lo_*  ci_up90_* ci_lo90_* se_* sig_*
order irf_* ci_up_* ci_lo_*  time ci_up90_* ci_lo90_* sig_*
drop if time == . 
gen zero = 0 if time!=.
gen time0 = time
gen time1 = time -0.025
gen time2 = time + 0.025

local lsize "3"
local tsize "3.5"
local legsize "3"

//Foodstuffs
twoway  (line zero time, lcolor(black) lwidth(vthin)) ///
		(scatter irf_essentials_index time, color(black) msymbol(circle_hollow)) ///
		(line irf_essentials_index time, lcolor(black) lpattern(dot) lwidth(medium)) ///
		(scatter irf_nonessentials_index time, color(gs8) msymbol(diamond_hollow)) ///
		(line irf_nonessentials_index time, lcolor(gs8) lpattern(dot) lwidth(medium)) ///
		(scatter irf_processed_index time, color(gs5) msymbol(triangle_hollow)) ///
		(line irf_processed_index time, lcolor(gs5) lpattern(dot) lwidth(medium)) ///
		(scatter sig_essentials_index time, color(black) msymbol(circle)) ///
		(scatter sig_nonessentials_index time, color(gs8) msymbol(diamond)) ///
		(scatter sig_processed_index time, color(gs5) msymbol(triangle)), ///
		title("Foodstuffs", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-4(1)2, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(size(`legsize') symxsize(*.5) order(2 4 6) col(1) pos(7) ring(0) lab(2 "Grains & Vegetables") lab(4 "Animal products") lab(6 "Cheese & Wine")  region(color(white%0))) ///
		graphregion(color(white)) plotregion(color(white)) name(g1, replace) nodraw

		

	
//Consumer & intermediate goods
twoway  (line zero time, lcolor(black) lwidth(vthin)) ///
		(scatter irf_p_soap_index time, color(black) msymbol(circle_hollow)) ///
		(line irf_p_soap_index time, lcolor(black) lpattern(dot) lwidth(medium)) ///
		(scatter irf_p_oliveoil_index time2, color(gs8) msymbol(diamond_hollow)) ///
		(line irf_p_oliveoil_index time2, lcolor(gs8) lpattern(dot) lwidth(medium)) ///
		(scatter irf_p_candles_index time, color(gs5) msymbol(triangle_hollow)) ///
		(line irf_p_candles_index time, lcolor(gs5) lpattern(dot) lwidth(medium)) ///
		(scatter sig_p_soap_index time, color(black) msymbol(circle)) ///
		(scatter sig_p_oliveoil_index time2, color(gs8) msymbol(diamond)) ///
		(scatter sig_p_candles_index time, color(gs5) msymbol(triangle)), ///
		title("Consumer & intermediate goods", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-4(1)2, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(size(`legsize') symxsize(*.5) order(2 4 6) col(1) pos(7) ring(0) lab(2 "Soap") lab(4 "Olive oil") lab(6 "Candles")  region(color(white%0))) ///
		graphregion(color(white)) plotregion(color(white)) name(g3, replace) nodraw
		
		
//Textile industry: inputs/intermediate
twoway  (line zero time, lcolor(black) lwidth(vthin)) ///
		(scatter irf_p_uwo_ns_index time, color(black) msymbol(circle_hollow)) ///
		(line irf_p_uwo_ns_index time, lcolor(black) lpattern(dot) lwidth(medium)) ///
		(scatter irf_p_threads_index time2, color(gs8) msymbol(diamond_hollow) jitter(1)) ///
		(line irf_p_threads_index time2, lcolor(gs8) lpattern(dot) lwidth(medium) jitter(1)) ///
		(scatter sig_p_uwo_ns_index time, color(black) msymbol(circle)) ///
		(scatter sig_p_threads_index time2, color(gs8) msymbol(diamond) jitter(1)), ///
		title("Textile industry: inputs & intermediate", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-4(1)2, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(size(`legsize') symxsize(*.5) order(2 4) col(1) pos(11) ring(0) lab(2 "Wool (unwashed)") lab(4 "Threads") region(color(white%0))) ///
		graphregion(color(white)) plotregion(color(white)) name(g2, replace) nodraw
		
		
//Heating
twoway  (line zero time, lcolor(black) lwidth(vthin)) ///
		(scatter irf_p_firewood_index time, color(black) msymbol(circle_hollow)) ///
		(line irf_p_firewood_index time, lcolor(black) lpattern(dot) lwidth(medium)) ///
		(scatter irf_p_charcoal_index time, color(gs8) msymbol(diamond_hollow)) ///
		(line irf_p_charcoal_index time, lcolor(gs8) lpattern(dot) lwidth(medium)) ///
		(scatter sig_p_firewood_index time, color(black) msymbol(circle)) ///
		(scatter sig_p_charcoal_index time, color(gs8) msymbol(diamond)), ///
		title("Heating", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-4(1)2, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(size(`legsize') symxsize(*.5) order(2 4) col(1) pos(7) ring(0) lab(2 "Firewood") lab(4 "Charcoal") region(color(white%0))) ///
		graphregion(color(white)) plotregion(color(white)) name(g4, replace) nodraw
		
		
//Metals industry
twoway  (line zero time, lcolor(black) lwidth(vthin)) ///
		(scatter irf_p_nai_index time, color(black) msymbol(circle_hollow)) ///
		(line irf_p_nai_index time, lcolor(black) lpattern(dot) lwidth(medium)) ///
		(scatter irf_p_iron time, color(gs8) msymbol(diamond_hollow)) ///
		(line irf_p_iron time, lcolor(gs8) lpattern(dot) lwidth(medium)) ///
		(scatter sig_p_nai_index time, color(black) msymbol(circle)) ///
		(scatter sig_p_iron time, color(gs8) msymbol(diamond)), ///
		title("Metals industry", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-6(2)2, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(size(`legsize') symxsize(*.5) order(2 4) col(1) pos(11) ring(0) lab(2 "Iron nails") lab(4 "Iron") region(color(white%0))) ///
		graphregion(color(white)) plotregion(color(white)) name(g5, replace) nodraw
		
		
		//Textile industry 
twoway  (line zero time, lcolor(black) lwidth(vthin)) ///
		(scatter irf_p_fine_index time, color(black) msymbol(circle_hollow)) ///
		(line irf_p_fine_index time, lcolor(black) lpattern(dot) lwidth(medium)) ///
		(scatter irf_p_coarse_index time, color(gs8) msymbol(diamond_hollow)) ///
		(line irf_p_coarse_index time, lcolor(gs8) lpattern(dot) lwidth(medium)) ///
		(scatter sig_p_fine_index time, color(black) msymbol(circle)) ///
		(scatter sig_p_coarse_index time, color(gs8) msymbol(diamond)), ///
		title("Textile industry", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-4(1)3, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(size(`legsize') symxsize(*.5) order(2 4) col(1) pos(11) ring(0) lab(2 "Fine cloths") lab(4 "Coarse cloths") region(color(white%0))) ///
		graphregion(color(white)) plotregion(color(white)) name(g6, replace) nodraw



		
} // qui


graph combine g1 g4 g3 g2 g6 g5, name(LP_disag_prices, replace) cols(3) imargin(medlarge) plotregion(color(white)) graphregion(color(white)) 
graph display LP_disag_prices, ysize(14) xsize(22)
graph export "results/Figure5.pdf", replace 




	

*_________________________________________

			

			
